#include "yideng.h"
#include "ui_yideng.h"
#include<QPushButton>
#include<QVBoxLayout>
#include<QTimer>

yideng::yideng(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::yideng)
    , scene(new QGraphicsScene(this))
    , yiBackground(nullptr)
    ,ClassifyBk(nullptr)
    ,felyne(nullptr)
    ,complain(nullptr)
{
    ui->setupUi(this);
    setupydBk();
    setupFelyne();
    dialog();
}

yideng::~yideng()
{
    delete scene;
    delete ui;
    delete felyne;
    qDeleteAll(dragons);
    dragons.clear();
    delete complain;
}
void yideng::setupydBk()
{
    ui->graphicsView->setScene(scene);
    setWindowTitle("研究所人员好像中了奇怪的病毒喵...");

    this->setFixedSize(1200, 760); //1800/1.5, 1140/1.5 和缩放抵消
    ui->graphicsView->setFixedSize(1200, 760);
    scene->setSceneRect(0, 0, 1800, 1140);
    ui->graphicsView->setTransform(QTransform::fromScale(1200.0/1800.0, 760.0/1140.0));

    yiBackground = new QGraphicsPixmapItem(QPixmap(":/image/C:/Users/lenovo/Pictures/yideng.jpg"));//背景

    yiBackground->setPos(0, 0);
    yiBackground->setZValue(-10);
    scene->addItem(yiBackground);

    ui->lsh->hide();
    ui->gds->hide();
    ui->ljj->hide();
    ui->zqg->hide();
    ui->dyz->hide();

    ui->obj->move(1000,600);
    connect(ui->obj, &QPushButton::clicked, this, &yideng::checkInteract);

}

void yideng::setupFelyne(){

    felyne = new DragFelyne(":/image/guzhang-unscreen.gif", this);
    felyne->move(200, 200);

    paths={
        ":/image/caizhua.png",//瘴气之谷
        ":/image/shitao.png",//瘴气之谷
        ":/image/guchui.png",//瘴气之谷
        ":/image/dazei.png",//古代树
        ":/image/huolong.png",//古代树
        ":/image/duyao.png",//古代树
        ":/image/fengpiao.png",//陆珊瑚
        ":/image/fukong.png",//陆珊瑚
        ":/image/yinghuo.png",//陆珊瑚
        ":/image/xuan.png",//陆珊瑚
        ":/image/gang.png",//龙结晶
        ":/image/yanfei.png",//龙结晶
        ":/image/miejin.png",//龙结晶
        ":/image/niyu.png",//大蚁冢
        ":/image/tusha.png"//大蚁冢

    };

    for(int i=0;i<15;i++){
        int q=i%5;
        int p=i/5;
        DragFelyne *dragon = new DragFelyne(paths[i], this);
        dragon->setGeometry(390+150*q,320+140*p,140,140);
        dragon->setScaledContents(true);//自适应
        dragon->hide();
        dragons.append(dragon);
    }


}
void yideng::dialog(){
    this->book=QRect(50,700,300,300);//1200
    connect(felyne, &DragFelyne::released, [&](){
    if(book.contains(felyne->releasePos)) {
    QDialog complain(this);
    complain.setWindowTitle("艾露猫的不满");
    complain.setFixedSize(150,90);
    QVBoxLayout lay(&complain);
    QLabel *text = new QLabel(QString("老大我不想看书喵"));
    QPushButton *right = new QPushButton("好吧");
    QPushButton *study = new QPushButton("要学习一点知识哦");
    lay.addWidget(text);
    lay.addWidget(right);
    lay.addWidget(study);

    connect(right, &QPushButton::clicked, &complain, &QDialog::close);
    connect(study, &QPushButton::clicked, &complain, &QDialog::close);

        complain.exec();
    }});

}

void yideng::checkInteract(){
    // gameTimer->stop();
    ClassifyBk = new QGraphicsPixmapItem(QPixmap(":/image/1745406419303.png"));//背景
    ClassifyBk->setPos(0, 0);
    scene->addItem(ClassifyBk);
    ClassifyBk->setZValue(-9);
    QRect lshh(550,200,100,100);
    QRect gdss(750,200,100,100);
    QRect ljjj(550,50,100,100);
    QRect dyzz(750,50,100,100);
    QRect zqgg(950,200,100,100);
    ui->lsh->setGeometry(lshh);//1200
    ui->ljj->setGeometry(ljjj);//1200
    ui->zqg->setGeometry(zqgg);//1200
    ui->dyz->setGeometry(dyzz);//1200
    ui->gds->setGeometry(gdss);//1200
    ui->lsh->show();
    ui->gds->show();
    ui->ljj->show();
    ui->zqg->show();
    ui->dyz->show();

    for (DragFelyne *dragon : dragons) {
        dragon->show();
    }

    // 连接每个dragon的释放信号
    for (DragFelyne *dragon : dragons) {
        connect(dragon, &DragFelyne::released, this, [=]() {
            QPoint center = dragon->pos() + QPoint(dragon->width() / 2, dragon->height() / 2);
            bool isInCorrectArea = false;

            // 检查是否在正确的区域
            switch (dragons.indexOf(dragon)) {
            case 0: case 1: case 2:  // 瘴气之谷的龙
                isInCorrectArea = zqgg.contains(center);
                break;
            case 3: case 4: case 5:  // 古代树森林的龙
                isInCorrectArea = gdss.contains(center);
                break;
            case 6: case 7: case 8: case 9:  // 陆珊瑚台地的龙
                isInCorrectArea = lshh.contains(center);
                break;
            case 10: case 11: case 12:  // 龙结晶之地的龙
                isInCorrectArea = ljjj.contains(center);
                break;
            case 13: case 14:  // 大蚁冢荒地的龙
                isInCorrectArea = dyzz.contains(center);
                break;
            }

            if (!isInCorrectArea) {
                dragon->move(500,500);
            }
        });
    }




}

